package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.qdlc.p2p.biz.service.AdditionalUserMovieTicketService;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.AdditionalUserMovieTicketDao;
import com.qdlc.p2p.dal.dto.AdditionalUserMovieTicket;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.AdditionalUserMovieTicketModel;

/**
 * Created by liaoxiangkai on 2015/11/24.
 */
@Service("additionalUserMovieTicketService")
public class AdditionalUserMovieTicketServiceImpl implements AdditionalUserMovieTicketService {

    @Resource
    private AdditionalUserMovieTicketDao additionalUserMovieTicketDao;

    @Override
    public PageDataList<AdditionalUserMovieTicketModel> getUserMovieTicketList(User user, int pageNum, int pageSize, int status, String platform) {
        PageDataList<AdditionalUserMovieTicket> dataList = additionalUserMovieTicketDao.getUserMovieTicketList(user, pageNum, pageSize, status);
        PageDataList<AdditionalUserMovieTicketModel> pageList_ = new PageDataList<AdditionalUserMovieTicketModel>();
        List<AdditionalUserMovieTicketModel> list = new ArrayList<AdditionalUserMovieTicketModel>();
        if (!dataList.getList().isEmpty() && dataList.getList().size() > 0) {
            for (AdditionalUserMovieTicket ticket : dataList.getList()) {
                AdditionalUserMovieTicketModel model = AdditionalUserMovieTicketModel.instance(ticket);
                if (StringUtils.isEmpty(platform)) {
                    platform = SystemConfigHelper.getValue(SystemConfigHelper.Nid.EXCHANGE_TICKET_PLATFORM_WEB);
                }
                //线下兑换链接参数：channel_id，sign，userid，code_number，orderNo，username，platform
                if (ticket.getStatus() == AdditionalUserMovieTicketModel.USER_MOVIETICKE_UNUSED || ticket.getStatus() == AdditionalUserMovieTicketModel.USER_MOVIETICKE_USED) {
                    StringBuilder sb = new StringBuilder("channel_id=");
                    sb.append(model.getPartnerId()).append("&userid=").append(model.getUser().getUserId())
                            .append("&code_number=").append(model.getExchangeCode())
                            .append("&orderNo=").append(model.getOrderNo())
                            .append("&username=").append(model.getUser().getUserName())
                            .append("&platform=").append(platform)
                            .append("&sign=").append(AdditionalUserMovieTicketModel.sign());
                    model.setExchangeURL(sb.toString());
                }
                list.add(model);
            }
        }
        pageList_.setList(list);
        pageList_.setPage(dataList.getPage());
        return pageList_;
    }

    @Override
    public PageDataList<AdditionalUserMovieTicketModel> list(AdditionalUserMovieTicketModel model) {
        return additionalUserMovieTicketDao.list(model);
    }

    @Override
    public AdditionalUserMovieTicket save(AdditionalUserMovieTicket additionalUserMovieTicket) {
        return additionalUserMovieTicketDao.save(additionalUserMovieTicket);
    }

    @Override
    public int updateByOrderNo(String orderNo, String partnerId, String exchangeCode, long userId) {
        return additionalUserMovieTicketDao.updateByOrderNo(orderNo, partnerId, exchangeCode, userId);
    }

    @Override
    public int updateByExchangeCode(long userId, String code, int status) {
        return additionalUserMovieTicketDao.updateByExchangeCode(userId, code, status);
    }

    @Override
    public void requestPHPInterface() {
        QueryParam param = QueryParam.getInstance();
        param.addParam("status", AdditionalUserMovieTicketModel.USER_MOVIETICKE_REQUEST);
        List<AdditionalUserMovieTicket> list = additionalUserMovieTicketDao.findByCriteria(param);
        if (!list.isEmpty() && list.size() > 0) {
            String url = SystemConfigHelper.getValue(SystemConfigHelper.Nid.REQUEST_PHP_DOMAIN) + "/Film/index.html";
            for (AdditionalUserMovieTicket ticket : list) {
                NameValuePair[] data = {new NameValuePair("mobile", ticket.getUser().getMobilePhone()), new NameValuePair("orderNo", ticket.getOrderNo()),
                        new NameValuePair("userid", String.valueOf(ticket.getUser().getUserId()))};
                String result = AdditionalUserMovieTicketModel.requestPHP(url, data);
                if ("0".equals(JSONObject.parseObject(result).get("code")))
                    additionalUserMovieTicketDao.updataById(ticket.getId(), AdditionalUserMovieTicketModel.USER_MOVIETICKE_WAIT);
            }
        }
    }
}
